<?php
$isReloadable = false;
require_once("../../../pers/inc.config.php");
require_once('../../_inc/inc.session.php');
require_once('../../_inc/funct.html.php');

$sqlSel = "SELECT d.*, s.cod_sez_rque, s.descr, m.cod_mod_rque, m.descr AS m_descr "
		." FROM rgl_mod_rque_dom d "
		." INNER JOIN rgl_mod_rque_sez s ON s.id_sez_rque = d.id_sez_rque "
		." INNER JOIN rgl_mod_rque m ON m.id_mod_rque = s.id_mod_rque" 
		." WHERE d.id_sez_rque = ".intval(reqVar('id'))
		." AND d.cod_dom_rque = '".reqVarAS('cd')."'";
$objDB->query($sqlSel,'selRec');
if (!$acDom = $objDB->fetch('selRec')) {
	backOnErr("var. id. '".reqVar('id').'/'.reqVar('cd')."' mancante o errato");
}

if (reqVar('azione') == 'salva') {
	$msgPag = confIns();
} 
reqDef('domanda',$acDom['domanda']);
reqDef('cod_dom_rque',$acDom['cod_dom_rque']);
reqDef('peso',$acDom['peso']);
reqDef('seq',$acDom['seq']);
$aRisp = unserialize($acDom['risposte']);
foreach($aRisp as $kX => $aX) {
	reqDef('dom_risp_'.$kX,$aX[0]);
	reqDef('dom_punt_'.$kX,$aX[1]);
}

testaHtml('Gestione modelli',$msgPag,'modl');
?>
<script type="text/javascript">
<!--
function submAz(az) {
	document.frm1.azione.value = az;
	document.frm1.submit();
}
//-->
</script>
<table border="0" cellpadding="3" cellspacing="1" width="100%" align='center'>
   <tr>
      <td class="cmd1" align="right">
         &nbsp;&nbsp;&nbsp;
         <a class="cmd1" href="javascript:submAz('salva');" >salva</a>
			<? echo cmdTorna('torna'); ?>
      </td>
   </tr>
</table>
<?
echo getHtmlForm('frm1','',array('enctype'=>true));
echo getHtmlHidden('azione','...');
echo getHtmlHidden('xsn',XSN);
echo getHtmlHidden('id');
echo getHtmlHidden('cd');
?>
<table class="tab1" border="0" cellpadding="3" cellspacing="1" width="100%" align='center'>
	<?=getHtmlRigaRif();?>
   <tr>
      <td class="tit1" colspan='20' align='center'>Domanda questionario</td>
   </tr>
   <tr>
      <td class="lab1" colspan='2' align='left'>Modello</td>
      <td class="txt1" colspan='2' align='left'><?=$acDom['cod_mod_rque']?></td>
      <td class="txt1" colspan='16' align='left'><?=$acDom['m_descr']?></td>
   </tr>
   <tr>
      <td class="lab1" colspan='2' align='left'>Sezione</td>
      <td class="txt1" colspan='2' align='left'><?=$acDom['cod_sez_rque']?></td>
      <td class="txt1" colspan='16' align='left'><?=$acDom['descr']?></td>
   </tr>
   <tr>
      <td class="lab1" colspan='2' align='left'>Domanda</td>
      <td class="txt1" colspan='18' align='left'>
      	<?=getHtmlTextArea('domanda',150,2)?>
      </td>
   </tr>
   <tr>
      <td class="lab1" colspan='2' align='left'>Sequenza</td>
      <td class="txt1" colspan='3' align='left'>
      	<?=getHtmlText('seq',4,6)?>
      </td>
      <td class="lab1" colspan='2' align='left'>Codice</td>
      <td class="txt1" colspan='5' align='left'>
      	<?=getHtmlText('cod_dom_rque',16,16)?>
      </td>
      <td class="lab1" colspan='2' align='left'>Peso</td>
      <td class="txt1" colspan='6' align='left'>
      	<?=getHtmlText('peso',6,6)?>
      </td>
	</tr>
   <tr>
      <td class="lab1" colspan='18' align='center'>Risposta</td>
      <td class="lab1" colspan='2' align='center'>Punti</td>
   </tr>
<?
for($nn=0;$nn<12;$nn++) {
?>
   <tr>
      <td class="txt1" colspan='18' align='center'>
      	<?=getHtmlText('dom_risp_'.$nn,120)?>
		</td>
      <td class="txt1" colspan='2' align='center'>
      	<?=getHtmlText('dom_punt_'.$nn,3,3)?>
		</td>
   </tr>
<?
}
?>
</table>
<table border="0" cellpadding="3" cellspacing="1" width="100%" align='center'>
   <tr>
      <td class="cmd1" align="right">
         &nbsp;&nbsp;&nbsp;
         <a class="cmd1" href="javascript:submAz('salva');" >salva</a>
			<? echo cmdTorna('torna'); ?>
      </td>
   </tr>
</table>
<?
echo "</form>\n";
codaHtml();
exit();

function confIns() {
	global $objDB;
	$msg = '';

	$_REQUEST['cod_dom_rque'] = strtoupper(trim(reqVar('cod_dom_rque')));
	if (reqVar('cod_dom_rque')=='') {
		$msg .= _t("Codice obbligatorio")."<br>";
	}
	if (preg_match("/[^_A-Z0-9]/",reqVar('cod_dom_rque'))) {
		$msg = _t("Codice '%s' puo' contenere solo lettere numeri e '-'",reqVar('cod_dom_rque'))."<br>";
	}
	if (preg_match("/[^0-9]/",reqVar('seq'))) {
		$msg = _t("Sequenza non numerica")."<br>";
	}
	if (preg_match("/[^0-9]/",reqVar('peso'))) {
		$msg = _t("Peso non numerico")."<br>";
	}
	if (trim(reqVar('domanda'))=='') {
		$msg .= "Domanda obbligatoria<br>";
	}
	if (reqVarAS('cod_dom_rque') != reqVarAS('cd')) {
		$sqlSel = "SELECT cod_dom_rque FROM rgl_mod_rque_dom " .
				" WHERE id_sez_rque = ".reqVar('id')." AND cod_dom_rque = '".reqVarAS('cod_dom_rque')."'";
		$objDB->exec($sqlSel);
		$aRow = $objDB->fetch();
		if ($aRow) {
			$msg .= _t("Codice gia' esistente")."<br>";
		}
	}
	$aRisp = array();
	for($nn=0;$nn<12;$nn++) {
		if (preg_match("/[^0-9]/",reqVar('dom_punt_'.$nn))) {
			$msg = _t("Punteggio $nn non numerico")."<br>";
		}
		if (reqVarInt('dom_punt_'.$nn) < 0 || reqVarInt('dom_punt_'.$nn)>100) {
			$msg = _t("Punteggio $nn deve essere tra 0 e 100")."<br>";
		}
		if (reqVarTrim('dom_risp_'.$nn)!='') {
			$aRisp[] = array(reqVarTrim('dom_risp_'.$nn),reqVarTrim('dom_punt_'.$nn));
		}
	}
	if (count($aRisp) < 2) {
		$msg .= "definire almeno 2 risposte<br>";
	}
	if ($msg != '') {
		return $msg;
	}
	$sSqlIns = "UPDATE rgl_mod_rque_dom SET domanda = '".reqVarAS('domanda')."'"
		.", seq = ".reqVarInt('seq')
		.", peso = ".reqVarInt('peso')
		.", risposte = '".addslashes(serialize($aRisp))."'"
		." WHERE id_sez_rque = ".reqVar('id')." AND cod_dom_rque = '".reqVarAS('cod_dom_rque')."'";
	$objDB->exec($sSqlIns,'insRec');
	$newId = $objDB->lastInsertId('insRec');
	
	header("Location: ".urlTorna("Domanda '".reqVar('cod_dom_rque')."' modificata"));
	exit();
}
